package controle.gui.cadastro;

import java.awt.AWTKeyStroke;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.Map.Entry;
import java.util.Random;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import java.awt.BorderLayout;
import java.sql.Date;
import java.text.ParseException;

import javax.swing.JTextField;
import javax.swing.JLabel;

import javax.swing.border.BevelBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.JButton;



import javax.swing.JCheckBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.InputEvent;

import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.text.MaskFormatter;
import javax.swing.JComboBox;
import javax.swing.JTabbedPane;
import javax.swing.border.EtchedBorder;

import javax.swing.border.TitledBorder;

import com.toedter.calendar.JDateChooser;

import controle.mascara.Mascara;
import controle.valida.ValidaDocumento;
import controle.valida.campo.IntegerDocument;
import controle.valida.campo.StringDocument;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import logica.jtext.FixedLengthDocument;
import logica.jtext.FormattedTextFieldVerifier;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.IOException;
import java.lang.management.ThreadMXBean;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.DefaultComboBoxModel;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JTree;
import javax.swing.JPopupMenu;
import java.awt.Component;
import javax.swing.JMenuItem;




public class Socio extends JInternalFrame  {
	private JTextField tfCod;
	private JFormattedTextField ftCpf;
	private JTextField jtRg;
	private JTextField JtEmissor;
	private JTextField jtEndereco;
	private JTextField jtNumero;
	private JTextField jtComplemento;
	private JTextField jtBairro;
	private JFormattedTextField ftCep;
	private JTextField jtNome;
	private JLabel lblCpfInvalido;
	private JTextField jtCod;
	private JFormattedTextField ftCnpj;
	private JTextField jtRSocial;
	private JLabel lblInvalido;
	private JComboBox cbEstado;
	private JComboBox cbCidade;
	private JDateChooser dcDataExpedicao;
    private	JDateChooser dcNascimento;
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Socio frame = new Socio();

					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @throws ParseException
	 */
	public Socio() throws ParseException {
		super("Modulo (Cadastro Sócios)", true, true, false, true);
		addInternalFrameListener(new InternalFrameAdapter() {

			public void internalFrameOpened(InternalFrameEvent e) {
			}
		});
		setBounds(100, 100, 683, 481);

		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null,
				null, null));
		panel_1.setBounds(317, 187, 97, -61);
		panel.add(panel_1);

		JButton btnSair = new JButton("Sair");
		btnSair.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnSair.setBounds(591, 414, 82, 24);
		panel.add(btnSair);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 12, 673, 390);
		panel.add(tabbedPane);

		JPanel panel_2 = new JPanel();
		tabbedPane.addTab("P. Fisica", null, panel_2, null);
		panel_2.setLayout(null);

		JPanel panel_4 = new JPanel();
		panel_4.setBorder(new TitledBorder(null, "Dados Principais",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_4.setBounds(12, 12, 645, 123);
		panel_2.add(panel_4);
		panel_4.setLayout(null);

		tfCod = new JTextField();
		tfCod.setBounds(12, 28, 64, 23);
		panel_4.add(tfCod);
		tfCod.setEnabled(false);
		tfCod.setEditable(false);
		tfCod.setColumns(10);

		JLabel lblCod = new JLabel("Cod");
		lblCod.setBounds(14, 12, 70, 14);
		panel_4.add(lblCod);

		this.ftCpf = new JFormattedTextField(Mascara.cpfMask());
		ftCpf.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				
				
					}  
	
	
			
		});
		ftCpf.setToolTipText("CPF Somente Numeros");

		this.ftCpf.setInputVerifier(new FormattedTextFieldVerifier());

		ftCpf.addFocusListener(new FocusAdapter() {
			@Override
			// au perder o focu
			public void focusLost(FocusEvent e) {
				validaCpf();

			}
		});
		ftCpf.setBounds(91, 28, 114, 23);
		panel_4.add(ftCpf);

		// tfNomeRazao.setColumns(10);

		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(223, 12, 70, 14);
		panel_4.add(lblNome);

		JLabel lblCpf = new JLabel("Cpf");
		lblCpf.setBounds(91, 12, 70, 14);
		panel_4.add(lblCpf);

		JLabel lblRg = new JLabel("Rg");
		lblRg.setBounds(12, 60, 70, 14);
		panel_4.add(lblRg);

		jtRg = new JTextField();
		jtRg.setBounds(14, 78, 147, 23);
		panel_4.add(jtRg);
		jtRg.setColumns(10);

		JtEmissor = new JTextField();
		JtEmissor.setBounds(169, 80, 114, 23);
		panel_4.add(JtEmissor);
		JtEmissor.setColumns(10);

		JLabel lblOEmissor = new JLabel("O. Emissor");
		lblOEmissor.setBounds(167, 60, 99, 14);
		panel_4.add(lblOEmissor);

		dcDataExpedicao = new JDateChooser();
		dcDataExpedicao.setBounds(291, 78, 114, 23);
		panel_4.add(dcDataExpedicao);

		JLabel lblDataExpedio = new JLabel("Data Expedição");
		lblDataExpedio.setBounds(291, 60, 91, 14);
		panel_4.add(lblDataExpedio);

		dcNascimento = new JDateChooser();
		dcNascimento.setBounds(417, 78, 107, 23);
		panel_4.add(dcNascimento);

		JLabel lblNascimento = new JLabel("Nascimento");
		lblNascimento.setBounds(417, 60, 94, 14);
		panel_4.add(lblNascimento);

		jtNome = new JTextField();
		jtNome.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				jtNome.setText(jtNome.getText().toUpperCase());
			}
		});
		jtNome.setBounds(223, 28, 410, 23);
		panel_4.add(jtNome);
		jtNome.setDocument(new StringDocument(100));
		jtNome.setColumns(10);

		lblCpfInvalido = new JLabel("Invalido");
		lblCpfInvalido.setVisible(false);
		lblCpfInvalido.setForeground(Color.RED);
		lblCpfInvalido.setBounds(118, 12, 87, 14);
		panel_4.add(lblCpfInvalido);

		JPanel pane_complementar = new JPanel();

		pane_complementar.setBorder(new TitledBorder(null,
				"Dados Complementares", TitledBorder.LEADING, TitledBorder.TOP,
				null, null));
		pane_complementar.setBounds(12, 147, 645, 205);
		panel_2.add(pane_complementar);
		pane_complementar.setLayout(null);

		cbCidade = new JComboBox();
		cbCidade.setBounds(229, 115, 170, 23);
		pane_complementar.add(cbCidade);

		JComboBox jcEstado = new JComboBox();
		jcEstado.addItem("Selecione");
		jcEstado.setBounds(12, 24, 134, 23);
		pane_complementar.add(jcEstado);

		jtEndereco = new JTextField();
		jtEndereco.setDocument(new FixedLengthDocument(70));
		jtEndereco.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				jtEndereco.setText(jtEndereco.getText().toUpperCase());
			}
		});
		jtEndereco.setDocument(new StringDocument(100));
		jtEndereco.setBounds(158, 26, 475, 23);
		pane_complementar.add(jtEndereco);
		jtEndereco.setColumns(10);

		JLabel lblN = new JLabel("N°");
		lblN.setBounds(12, 59, 70, 14);
		pane_complementar.add(lblN);

		JLabel lblComplemento = new JLabel("Complemento");
		lblComplemento.setBounds(103, 59, 111, 14);
		pane_complementar.add(lblComplemento);

		jtNumero = new JTextField();
		jtNumero.setBounds(12, 76, 76, 23);
		pane_complementar.add(jtNumero);
		jtNumero.setColumns(10);

		jtComplemento = new JTextField();
		jtComplemento.setDocument(new FixedLengthDocument(60));
		jtComplemento.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				jtComplemento.setText(jtComplemento.getText().toUpperCase());
			}
		});
		jtComplemento.setBounds(103, 76, 332, 23);
		pane_complementar.add(jtComplemento);
		jtComplemento.setColumns(10);

		JLabel lblBairro = new JLabel("Bairro");
		lblBairro.setBounds(449, 59, 70, 14);
		pane_complementar.add(lblBairro);

		jtBairro = new JTextField();
		jtBairro.setBounds(447, 76, 186, 23);
		pane_complementar.add(jtBairro);
		jtBairro.setColumns(10);

		JLabel lblCep = new JLabel("Cep");
		lblCep.setBounds(12, 103, 70, 14);
		pane_complementar.add(lblCep);

		JLabel lblEstado = new JLabel("Estado");
		lblEstado.setBounds(146, 100, 70, 14);
		pane_complementar.add(lblEstado);

		JLabel lblCidade = new JLabel("Cidade");
		lblCidade.setBounds(229, 100, 70, 14);
		pane_complementar.add(lblCidade);

		cbEstado = new JComboBox();
		cbEstado.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				if(!e.getItem().toString().equals(" ")){
					cbCidade.setEnabled(true);
					
				}else{
					cbCidade.setEnabled(false);
				}
			}
		});
		cbEstado.setModel(new DefaultComboBoxModel(new String[] {" ", "jorge"}));
	   cbEstado.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				
			}
		});
		cbEstado.setBounds(147, 115, 57, 23);
		pane_complementar.add(cbEstado);

		ftCep = new JFormattedTextField(Mascara.cepMask());
		ftCep.setBounds(12, 117, 122, 23);
		pane_complementar.add(ftCep);

		JButton btnNovo = new JButton("Novo");
		btnNovo.setMnemonic('a');
		btnNovo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				ablitaCampoCpf();
			}
		});
		btnNovo.setBounds(39, 169, 117, 24);
		pane_complementar.add(btnNovo);

		JButton btnAlterar = new JButton("Alterar");
		btnAlterar.setBounds(192, 169, 117, 24);
		pane_complementar.add(btnAlterar);

		JButton btnSalvar = new JButton("Salvar");
		btnSalvar.setBounds(340, 169, 117, 24);
		pane_complementar.add(btnSalvar);

		JButton btnExcluir = new JButton("Excluir");
		btnExcluir.setBounds(489, 169, 117, 24);
		pane_complementar.add(btnExcluir);

		JPanel panel_3 = new JPanel();
		tabbedPane.addTab("P. Juridica", null, panel_3, null);
		panel_3.setLayout(null);

		JPanel panel_5 = new JPanel();
		panel_5.setBorder(new TitledBorder(null, "Dados Empresa",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_5.setBounds(12, 12, 644, 107);
		panel_3.add(panel_5);
		panel_5.setLayout(null);

		JLabel lblCod_1 = new JLabel("Cod");
		lblCod_1.setBounds(12, 12, 70, 14);
		panel_5.add(lblCod_1);

		jtCod = new JTextField();
		jtCod.setEditable(false);

		jtCod.setBounds(12, 27, 70, 23);
		panel_5.add(jtCod);
		jtCod.setColumns(10);

		JLabel lblCnpj = new JLabel("Cnpj");
		lblCnpj.setBounds(94, 12, 70, 14);
		panel_5.add(lblCnpj);

		ftCnpj = new JFormattedTextField(new Mascara().cnpjMask());
		ftCnpj.addFocusListener(new FocusAdapter() {
			@Override
			// au perder o focu
			public void focusLost(FocusEvent e) {
				validaCnpj();

			}
		});
		
			
		
		
		ftCnpj.setBounds(94, 27, 136, 23);
		panel_5.add(ftCnpj);
		
		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(ftCnpj, popupMenu);
		
		JMenuItem mntmPesquisar = new JMenuItem("Pesquisar");
		popupMenu.add(mntmPesquisar);

		JLabel lblRazoScial = new JLabel("Razão Sócial");
		lblRazoScial.setBounds(241, 12, 108, 14);
		panel_5.add(lblRazoScial);

		jtRSocial = new JTextField();
		jtRSocial.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				jtRSocial.setText(jtRSocial.getText().toUpperCase());
			}
		});
		jtRSocial.setDocument(new StringDocument(100));
		
		jtRSocial.setBounds(242, 27, 390, 23);
		panel_5.add(jtRSocial);
		jtRSocial.setColumns(10);
		
		lblInvalido = new JLabel("Invalido");
		lblInvalido.setVisible(false);
		lblInvalido.setForeground(Color.RED);
		lblInvalido.setBounds(131, 12, 70, 14);
		panel_5.add(lblInvalido);
		
		JLabel lblIEstadual = new JLabel("I. Estadual");
		lblIEstadual.setBounds(12, 54, 97, 14);
		panel_5.add(lblIEstadual);
		
		textField = new JTextField();
		textField.setBounds(12, 69, 114, 23);
		panel_5.add(textField);
		textField.setColumns(10);
		
		JLabel lblIMunicipal = new JLabel("I. Municipal");
		lblIMunicipal.setBounds(131, 54, 114, 14);
		panel_5.add(lblIMunicipal);
		
		textField_1 = new JTextField();
		textField_1.setBounds(131, 69, 114, 23);
		panel_5.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNire = new JLabel("Nire");
		lblNire.setBounds(252, 54, 70, 14);
		panel_5.add(lblNire);
		
		textField_2 = new JTextField();
		textField_2.setBounds(252, 69, 114, 23);
		panel_5.add(textField_2);
		textField_2.setColumns(10);
		
		JPanel panel_6 = new JPanel();
		panel_6.setBorder(new TitledBorder(null, "Dados Complementares", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_6.setBounds(12, 131, 644, 179);
		panel_3.add(panel_6);
		panel_6.setLayout(null);
		
		JComboBox comboBox = new JComboBox();
		comboBox.setBounds(12, 37, 142, 23);
		panel_6.add(comboBox);
		
		textField_3 = new JTextField();
		textField_3.setBounds(166, 37, 466, 23);
		panel_6.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblN_1 = new JLabel("N°");
		lblN_1.setBounds(12, 69, 70, 14);
		panel_6.add(lblN_1);
		
		textField_4 = new JTextField();
		textField_4.setBounds(12, 83, 59, 23);
		panel_6.add(textField_4);
		textField_4.setColumns(10);
		
		JLabel lblComplemeto = new JLabel("Complemeto");
		lblComplemeto.setBounds(84, 69, 88, 14);
		panel_6.add(lblComplemeto);
		
		textField_5 = new JTextField();
		textField_5.setBounds(83, 83, 345, 23);
		panel_6.add(textField_5);
		textField_5.setColumns(10);
		
		JLabel lblBairro_1 = new JLabel("Bairro");
		lblBairro_1.setBounds(443, 69, 70, 14);
		panel_6.add(lblBairro_1);
		
		textField_6 = new JTextField();
		textField_6.setBounds(442, 83, 190, 23);
		panel_6.add(textField_6);
		textField_6.setColumns(10);
		
		JFormattedTextField formattedTextField = new JFormattedTextField();
		formattedTextField.setBounds(12, 126, 105, 23);
		panel_6.add(formattedTextField);
		
		JLabel lblCep_1 = new JLabel("Cep");
		lblCep_1.setBounds(12, 110, 70, 14);
		panel_6.add(lblCep_1);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setBounds(129, 126, 59, 23);
		panel_6.add(comboBox_1);
		
		JComboBox comboBox_2 = new JComboBox();
		comboBox_2.setBounds(198, 126, 168, 23);
		panel_6.add(comboBox_2);
		
		JLabel lblEstado_1 = new JLabel("Estado");
		lblEstado_1.setBounds(129, 110, 70, 14);
		panel_6.add(lblEstado_1);
		
		JLabel lblCidade_1 = new JLabel("Cidade");
		lblCidade_1.setBounds(198, 110, 70, 14);
		panel_6.add(lblCidade_1);
		desabilitaCamposCpf();
	}

	public void validaCpf() {
		boolean cpf = ValidaDocumento.isValidCPF(ftCpf.getText().replace(".", "").replace("-", ""));

		if (cpf != true) {
			lblCpfInvalido.setVisible(true);
			ftCpf.grabFocus();

			System.out.println("teste");

		} else {
			lblCpfInvalido.setVisible(false);
			ftCpf.transferFocus();
		}
	}
	public void validaCnpj(){
		
		boolean cnpj = ValidaDocumento.isValidCNPJ(ftCnpj.getText().replace(".","").replace("/","").replace("-",""));
		
		if (cnpj != true) {
			lblInvalido.setVisible(true);
			ftCnpj.grabFocus();
			ftCnpj.setText("");

			System.out.println("teste");

		} else {
			lblInvalido.setVisible(false);
			ftCnpj.transferFocus();
		}
		
	}
	public void desabilitaCamposCpf(){
		this.jtComplemento.setEditable(false);
		this.jtEndereco.setEditable(false);
		this.jtNome.setEditable(false);
		this.jtBairro.setEditable(false);
		this.JtEmissor.setEditable(false);
		this.jtRg.setEditable(false);
		//this.cbCidade.setEditable(false);
		//this.cbEstado.setEditable(false);
	   this.ftCep.setEditable(false);
	   this.ftCpf.setEditable(false);
	   this.jtNumero.setEditable(false);
	   this.dcDataExpedicao.setEnabled(false);
	   this.dcNascimento.setEnabled(false);
	   this.cbCidade.setEnabled(false);
	   this.cbEstado.setEnabled(false);

		
	}
	private void ablitaCampoCpf(){
		this.jtComplemento.setEditable(true);
		this.jtEndereco.setEditable(true);
		this.jtNome.setEditable(true);
		this.jtBairro.setEditable(true);
		this.JtEmissor.setEditable(true);
		this.jtRg.setEditable(true);
		//this.cbCidade.setEditable(false);
		//this.cbEstado.setEditable(false);
	   this.ftCep.setEditable(true);
	   this.ftCpf.setEditable(true);
	   this.jtNumero.setEditable(true);
	   this.dcDataExpedicao.setEnabled(true);
	   this.dcNascimento.setEnabled(true);
	  // this.cbCidade.setEnabled(false);
	   this.cbEstado.setEnabled(true);

		
		
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
